import axios from "axios";
import {WebAdapterForUrl} from "./components/WebAdapterForUrl";
import {RequireAdapter} from "@/api/types";
import {Result} from "$/shared";

const interHandler: Record<string, (...args: any[]) => Promise<Result<any>>> = {
  '/api/http/request': (data: any) => {
    return axios.request(data);
  },
  ...WebAdapterForUrl
}

/**
 * 浏览器插件适配器
 */
export const WebAdapter: RequireAdapter<any> = async (url, data) => {
  if (!url) return Promise.reject(new Error('Axios config.url is required'));
  const func = interHandler[url];
  if (!func) {
    console.error(url, data);
    return Promise.reject(new Error(`Axios config.url ${url} is not supported`));
  }
  return await func(data);
}